//
//  JRMainTabBarVC.swift
//  LED OTA
//
//  Created by rui on 2024/4/12.
//

import UIKit
import SnapKit

open class JRMainTabBarVC: JRBaseVC {
    
    public var selectViewController: UIViewController? = nil
    
    public lazy var tabBar: UITabBar =  {
        let tabBar = UITabBar()
        tabBar.delegate = self
        view.addSubview(tabBar)
        return tabBar
    }()
    
    public var selectIndex: Int? {
        set {
            guard isViewLoaded else {
                return
            }
            guard let items:[UITabBarItem] = self.tabBar.items else {
                return
            }
            let existObj: Int = newValue ?? 0
            if items.count > existObj {
                let item = items[existObj]
                tabBar.selectedItem = item
                tabBar(tabBar, didSelect: item)
            }
        }
        get {
            guard let items:[UITabBarItem] = self.tabBar.items else {
                return nil
            }
            guard self.tabBar.selectedItem != nil else {
                return nil
            }
            return items.firstIndex(of: tabBar.selectedItem!)
        }
    }
    
    open override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        tabBar.snp.updateConstraints { make in
            make.height.equalTo(self.view.safeAreaInsets.bottom+49.0)
        }
        setNavigationBarConfig()
    }
    
    open func reloadNavigationBar() {
        setNavigationBarConfig()
    }
    
    func setNavigationBarConfig() {
        self.title = self.selectViewController?.title
        self.djr.setLeftBarButtons(self.selectViewController?.djr.getLeftBarButtomItems())
        self.djr.setRightBarButtons(self.selectViewController?.djr.getRightBarButtomItems())
        if (self.selectViewController != nil) && self.selectViewController!.isKind(of: JRBaseVC.self) {
            let basicVC = self.selectViewController! as! JRBaseVC
            self.navigationController?.setNavigationBarHidden(basicVC.isHiddenNavBar, animated: false)
            self.navBarImage = basicVC.navBarImage;
        }
    }

}

extension JRMainTabBarVC: UITabBarDelegate {
    public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if self.children.count == 0, self.children.count == 1 {
            return
        }
        guard let idx = tabBar.items?.firstIndex(of: item) else {
            return
        }
        let childVC = self.children[idx]
        if !childVC.isEqual(self.selectViewController) {
            self.selectViewController?.view.removeFromSuperview()
            self.view.insertSubview(childVC.view, at: 0);
            childVC.view.snp.remakeConstraints { make in
                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
                make.left.right.equalTo(self.view)
                make.bottom.equalTo(self.tabBar.snp.top)
            }
            self.selectViewController = childVC
            setNavigationBarConfig()
        }
    }

}
